// GroupMembership.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents the membership list of a Facebook group.
    /// </summary>
    public sealed class GroupMembership : Membership {

        private bool _membersTransformed;
        private bool _adminsTransformed;
        private bool _officersTransformed;
        private bool _notRepliedUsersTransformed;

        internal GroupMembership(Hashtable membershipState)
            : base(membershipState) {
        }

        /// <summary>
        /// The user IDs of administrators of a group.
        /// </summary>
        public ICollection<string> Admins {
            get {
                if (_adminsTransformed == false) {
                    return TransformAdmins();
                }
                return (ICollection<string>)this["admins"];
            }
        }

        /// <summary>
        /// The user IDs of members of a group.
        /// </summary>
        public ICollection<string> Members {
            get {
                if (_membersTransformed == false) {
                    return TransformMembers();
                }
                return (ICollection<string>)this["members"];
            }
        }

        /// <summary>
        /// The user IDs of members who haven't replied to a group invitation.
        /// </summary>
        public ICollection<string> NotRepliedUsers {
            get {
                if (_notRepliedUsersTransformed == false) {
                    return TransformNotRepliedUsers();
                }
                return (ICollection<string>)this["not_replied"];
            }
        }

        /// <summary>
        /// The user IDs of officers of a group.
        /// </summary>
        public ICollection<string> Officers {
            get {
                if (_officersTransformed == false) {
                    return TransformOfficers();
                }
                return (ICollection<string>)this["officers"];
            }
        }

        private ICollection<string> TransformAdmins() {
            Debug.Assert(_adminsTransformed == false);

            ArrayList adminListState = (ArrayList)this["admins"];
            ICollection<string> admins = TransformUserList(adminListState);

            this["admins"] = admins;
            _adminsTransformed = true;

            return admins;
        }

        private ICollection<string> TransformMembers() {
            Debug.Assert(_membersTransformed == false);

            ArrayList membersListState = (ArrayList)this["members"];
            ICollection<string> members = TransformUserList(membersListState);

            this["members"] = members;
            _membersTransformed = true;

            return members;
        }

        private ICollection<string> TransformNotRepliedUsers() {
            Debug.Assert(_notRepliedUsersTransformed == false);

            ArrayList notRepliedListState = (ArrayList)this["not_replied"];
            ICollection<string> notRepliedUsers = TransformUserList(notRepliedListState);

            this["not_replied"] = notRepliedUsers;
            _notRepliedUsersTransformed = true;

            return notRepliedUsers;
        }

        private ICollection<string> TransformOfficers() {
            Debug.Assert(_officersTransformed == false);

            ArrayList officersListState = (ArrayList)this["officers"];
            ICollection<string> officers = TransformUserList(officersListState);

            this["officers"] = officers;
            _officersTransformed = true;

            return officers;
        }
    }
}
